package com.lemon.smartsafety.ui.adapter.common;

import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.common.EvidenceDataI;
import com.lemon.smartsafety.entity.event.common.HorizontalEvidenceEvent;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.common.LemonFileHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.utils.photo.GlideImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;
import cn.finalteam.galleryfinal.widget.GFImageView;
import cn.finalteam.galleryfinal.widget.zoonview.Compat;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/30.
 */
public class HorizontalEvidenceAdapter extends LemonRcvAdapter<EvidenceDataI> {
    public HorizontalEvidenceAdapter(@Nullable List<EvidenceDataI> data) {
        super(data);
    }

    public static List<PhotoInfo> getPhotoInfoList(List<EvidenceDataI> data) {
        List<PhotoInfo> result = new ArrayList<>();
        if (data == null) {
            return result;
        }
        for (int i = 0; i < data.size(); i++) {
            EvidenceDataI evidenceDataI = data.get(i);
            if (LemonFileHelper.isImageUrl(evidenceDataI.getEvidenceUrl())) {
                PhotoInfo photoInfo = new PhotoInfo();
                photoInfo.setPhotoId(i);
                photoInfo.setPhotoPath(HttpUtils.formatFilesUrl(data.get(i).getEvidenceUrl()));
                result.add(photoInfo);
            }
        }
        return result;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new HorizontalEvidenceItem(mDataList);
    }

    public class HorizontalEvidenceItem implements AdapterItem<EvidenceDataI> {
        LinearLayout ripple;
        GFImageView ivPhoto;
        ImageView   ivDelete;

        private EvidenceDataI mData;
        private int           mPosition;

        private List<EvidenceDataI> mDatas;

        public HorizontalEvidenceItem(List<EvidenceDataI> datas) {
            this.mDatas = datas;
        }

        @Override
        public int getLayoutResId() {
            return R.layout.item_evidence_photo;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            ivPhoto = (GFImageView) root.findViewById(R.id.ivPhoto);
            ivDelete = (ImageView) root.findViewById(R.id.ivDelete);
            ivDelete.setVisibility(View.GONE);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HorizontalEvidenceEvent evidenceEvent = new HorizontalEvidenceEvent();
                    evidenceEvent.setTag(HorizontalEvidenceEvent.class.getName());
                    evidenceEvent.setData(mDatas);
                    evidenceEvent.setPosition(mPosition);
                    AppService.getBus().post(evidenceEvent);
                }
            });
       /*     ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    HorizontalEvidenceEvent evidenceEvent = new HorizontalEvidenceEvent();
                    evidenceEvent.setTag(HorizontalEvidenceEvent.class.getName());
                    evidenceEvent.setData(mDatas);
                    evidenceEvent.setPosition(mPosition);
                    AppService.getBus().post(evidenceEvent);
                }
            });*/
        }

        @Override
        public void handleData(EvidenceDataI data, int position) {
            mData = data;
            mPosition = position;
            try {
                if (LemonFileHelper.isVoiceUrl(data.getEvidenceUrl())) {
                    LogUtils.LOGE("语音图片");

                    //ivPhoto.setImageDrawable(ContextCompat.getDrawable(IApplication.context(),R.drawable.ic_settings_voice));
                    ivPhoto.setImageDrawable(ContextCompat.getDrawable(IApplication.context(),R.mipmap.icon_new_voice));

                   //ivPhoto.setImageResource(R.drawable.ic_settings_voice);
                }else{
                    if (StringUtils.nullValue(mData.getEvidenceUrl()).length() != 0) {
                        GlideImageLoader.loadImage(ivPhoto.getContext(), HttpUtils.formatFilesUrl(mData.getEvidenceUrl()), ivPhoto);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
